#ifndef CIPHER_H
#define CIPHER_H

#include <QObject>
#include <openssl/evp.h>

class CCipher : public QObject
{
    Q_OBJECT
public:
    explicit CCipher(QObject *parent = nullptr);  //屏蔽自动类型转换

    EVP_CIPHER_CTX *ctx;

    virtual bool encrypt(const QByteArray &in,QByteArray &out,int mode) = 0;
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode) = 0;  //纯虚函数
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode, const QByteArray &ivec) = 0;
    bool encryption(const QByteArray& in, QByteArray& out, const QByteArray& key, const QByteArray& ivec, const EVP_CIPHER *cipher, bool enc);

};

class CAes : public CCipher
{
    Q_OBJECT
public:
    explicit CAes(QObject *parent = nullptr);
    QByteArray ivec = QByteArray::fromHex("667b02a85c61c786def4521b060265e8");

    virtual bool encrypt(const QByteArray &in,QByteArray &out,int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode, const QByteArray &ivec);

};

class CDes : public CCipher
{
    Q_OBJECT
public:
    explicit CDes(QObject *parent = nullptr);
    QByteArray ivec = QByteArray::fromHex("667b02a85c61c786");

    virtual bool encrypt(const QByteArray &in,QByteArray &out,int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode, const QByteArray &ivec);

};


class C3Des : public CCipher
{
    Q_OBJECT
public:
    explicit C3Des(QObject *parent = nullptr);
    QByteArray ivec = QByteArray::fromHex("667b02a85c61c786");

    virtual bool encrypt(const QByteArray &in,QByteArray &out,int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode, const QByteArray &ivec);
    bool encryption(const QByteArray& in, QByteArray& out, const QByteArray& key, const QByteArray& ivec, const EVP_CIPHER *cipher, bool enc);

};

class CHash : public CCipher
{
    Q_OBJECT
public:
    explicit CHash(QObject *parent = nullptr);

    virtual bool encrypt(const QByteArray &in,QByteArray &out,int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode);
    virtual bool encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode, const QByteArray &ivec);

};


#endif // CIPHER_H
